package array;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ArrayClass {


    public static int singleNumber(int[] nums) {


        int result = 0;
        for (int i = 0; i < nums.length; i++) {

            result = result ^ nums[i];


        }
        return result;

    }

    //无重复字符最长字串

    public static int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();

        List<String> strList = new ArrayList<String>();
        for(int i = 0;i<chars.length;i++){
            StringBuffer  newCharStr = new StringBuffer();
            newCharStr.append(chars[i]);
            for(int j = i+1;j<chars.length;j++){
                if(newCharStr.indexOf(String.valueOf(chars[j]))<0){
                    newCharStr.append(chars[j]);
                }else{
                    break;
                }
            }
            strList.add(newCharStr.toString());

        }

        String longestStr = "";

        for(String str:strList){

           if(str.length()>longestStr.length()){
               longestStr = str;
           }

        }
        System.out.println(longestStr);

        return longestStr.length();

    }


    public static int lengthOfLongestSubstring2(String calName) {
        if(calName==null||calName.length()==0){
            return 0;
        }

        if(calName.length()==1){
            return 1;
        }
        char[] chs = calName.toCharArray();
        int winStart = 0;
        int winEnd = 0;
        int lengthAll = 0;
        String strLast = "";
        String oldWinStr = calName.substring(winStart, winStart+1);
        while (winEnd < calName.length()-1) {

            if (oldWinStr.indexOf(chs[winEnd+1]) >= 0 && winStart <= winEnd) {
                winStart++;
                log.info("in if start={},end={},oldWinStr={}", winStart, winEnd, oldWinStr);
//                continue;
            } else if (winEnd < calName.length() - 1) {
                winEnd++;
            }
            oldWinStr = calName.substring(winStart, winEnd + 1);
            if (lengthAll < oldWinStr.length()) {
                lengthAll = oldWinStr.length();
                strLast = oldWinStr;
            }
            log.info("start={},end={},oldWinStr={}", winStart, winEnd, oldWinStr);
        }
        log.info("strLast={}", strLast);
        return lengthAll;


    }

    public static void main(String[] args) {
        int[] input = {4, 1, 2, 1, 2, 3, 3, 5, 5, 2, 5, 2, 5};

        System.out.println(singleNumber(input));


        String s = " ";
        System.out.println(lengthOfLongestSubstring2(s));

    }

}
